<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>FFT.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="FFT code in Java">
<meta NAME="TITLE" CONTENT="FFT code in Java">
<meta NAME="KEYWORDS" CONTENT="FFT,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>FFT.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "FFT.java">FFT.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac FFT.java</span>
<span class="comment"> *  Execution:    java FFT N</span>
<span class="comment"> *  Dependencies: Complex.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Compute the FFT and inverse FFT of a length N complex sequence.</span>
<span class="comment"> *  Bare bones implementation that runs in O(N log N) time. Our goal</span>
<span class="comment"> *  is to optimize the clarity of the code, rather than performance.</span>
<span class="comment"> *</span>
<span class="comment"> *  Limitations</span>
<span class="comment"> *  -----------</span>
<span class="comment"> *   -  assumes N is a power of 2</span>
<span class="comment"> *</span>
<span class="comment"> *   -  not the most memory efficient algorithm (because it uses</span>
<span class="comment"> *      an object type for representing complex numbers and because</span>
<span class="comment"> *      it re-allocates memory for the subarray, instead of doing</span>
<span class="comment"> *      in-place or reusing a single temporary array)</span>
<span class="comment"> *  </span>
<span class="comment"> *</span>
<span class="comment"> *  % java FFT 4</span>
<span class="comment"> *  x</span>
<span class="comment"> *  -------------------</span>
<span class="comment"> *  -0.03480425839330703</span>
<span class="comment"> *  0.07910192950176387</span>
<span class="comment"> *  0.7233322451735928</span>
<span class="comment"> *  0.1659819820667019</span>
<span class="comment"> *</span>
<span class="comment"> *  y = fft(x)</span>
<span class="comment"> *  -------------------</span>
<span class="comment"> *  0.9336118983487516</span>
<span class="comment"> *  -0.7581365035668999 + 0.08688005256493803i</span>
<span class="comment"> *  0.44344407521182005</span>
<span class="comment"> *  -0.7581365035668999 - 0.08688005256493803i</span>
<span class="comment"> *</span>
<span class="comment"> *  z = ifft(y)</span>
<span class="comment"> *  -------------------</span>
<span class="comment"> *  -0.03480425839330703</span>
<span class="comment"> *  0.07910192950176387 + 2.6599344570851287E-18i</span>
<span class="comment"> *  0.7233322451735928</span>
<span class="comment"> *  0.1659819820667019 - 2.6599344570851287E-18i</span>
<span class="comment"> *</span>
<span class="comment"> *  c = cconvolve(x, x)</span>
<span class="comment"> *  -------------------</span>
<span class="comment"> *  0.5506798633981853</span>
<span class="comment"> *  0.23461407150576394 - 4.033186818023279E-18i</span>
<span class="comment"> *  -0.016542951108772352</span>
<span class="comment"> *  0.10288019294318276 + 4.033186818023279E-18i</span>
<span class="comment"> *</span>
<span class="comment"> *  d = convolve(x, x)</span>
<span class="comment"> *  -------------------</span>
<span class="comment"> *  0.001211336402308083 - 3.122502256758253E-17i</span>
<span class="comment"> *  -0.005506167987577068 - 5.058885073636224E-17i</span>
<span class="comment"> *  -0.044092969479563274 + 2.1934338938072244E-18i</span>
<span class="comment"> *  0.10288019294318276 - 3.6147323062478115E-17i</span>
<span class="comment"> *  0.5494685269958772 + 3.122502256758253E-17i</span>
<span class="comment"> *  0.240120239493341 + 4.655566391833896E-17i</span>
<span class="comment"> *  0.02755001837079092 - 2.1934338938072244E-18i</span>
<span class="comment"> *  4.01805098805014E-17i</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">FFT</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides methods for computing the </span>
<span class="comment"> *  FFT (Fast-Fourier Transform), inverse FFT, linear convolution,</span>
<span class="comment"> *  and circular convolution of a complex array.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  It is a bare-bones implementation that runs in </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> log </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> time,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the length of the complex array. For simplicity,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> must be a power of 2.</span>
<span class="comment"> *  Our goal is to optimize the clarity of the code, rather than performance.</span>
<span class="comment"> *  It is not the most memory efficient implementation because it uses</span>
<span class="comment"> *  objects to represents complex numbers and it it re-allocates memory</span>
<span class="comment"> *  for the subarray, instead of doing in-place or reusing a single temporary array.</span>
<span class="comment"> *  </span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/99scientific"</span><span class="keyword">&gt;</span><span class="comment">Section 9.9</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">FFT</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Complex</span><span class="normal"> ZERO </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Complex</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// Do not instantiate.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">FFT</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the FFT of the specified complex array.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the complex array</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the FFT of the complex array </span><span class="keyword">&lt;tt&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the length of </span><span class="keyword">&lt;t&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is not a power of 2</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">Complex</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// base case</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">N </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> x</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// radix 2 Cooley-Tukey FFT</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">N </span><span class="symbol">%</span><span class="normal"> </span><span class="number">2</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"N is not a power of 2"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// fft of even terms</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> even </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> k </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> k </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">;</span><span class="normal"> k</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            even</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">*</span><span class="normal">k</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> q </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">even</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// fft of odd terms</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> odd  </span><span class="symbol">=</span><span class="normal"> even</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// reuse the array</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> k </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> k </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">;</span><span class="normal"> k</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            odd</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">*</span><span class="normal">k </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">odd</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// combine</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> k </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> k </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">;</span><span class="normal"> k</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">double</span><span class="normal"> kth </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">-</span><span class="number">2</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> k </span><span class="symbol">*</span><span class="normal"> Math</span><span class="symbol">.</span><span class="normal">PI </span><span class="symbol">/</span><span class="normal"> N</span><span class="symbol">;</span>
<span class="normal">            </span><span class="usertype">Complex</span><span class="normal"> wk </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Complex</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">cos</span><span class="symbol">(</span><span class="normal">kth</span><span class="symbol">),</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sin</span><span class="symbol">(</span><span class="normal">kth</span><span class="symbol">));</span>
<span class="normal">            y</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">]</span><span class="normal">       </span><span class="symbol">=</span><span class="normal"> q</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">].</span><span class="function">plus</span><span class="symbol">(</span><span class="normal">wk</span><span class="symbol">.</span><span class="function">times</span><span class="symbol">(</span><span class="normal">r</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">]));</span>
<span class="normal">            y</span><span class="symbol">[</span><span class="normal">k </span><span class="symbol">+</span><span class="normal"> N</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> q</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">].</span><span class="function">minus</span><span class="symbol">(</span><span class="normal">wk</span><span class="symbol">.</span><span class="function">times</span><span class="symbol">(</span><span class="normal">r</span><span class="symbol">[</span><span class="normal">k</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the inverse FFT of the specified complex array.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the complex array</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the inverse FFT of the complex array </span><span class="keyword">&lt;tt&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the length of </span><span class="keyword">&lt;t&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is not a power of 2</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">ifft</span><span class="symbol">(</span><span class="normal">Complex</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">// take conjugate</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">conjugate</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// compute forward FFT</span>
<span class="normal">        y </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// take conjugate again</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">conjugate</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// divide by N</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">scale</span><span class="symbol">(</span><span class="number">1.0</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> N</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the circular convolution of the two specified complex arrays.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x one complex array</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the other complex array</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the circular convolution of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">y</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the length of </span><span class="keyword">&lt;t&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> does not equal</span>
<span class="comment">     *         the length of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">y</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or if the length is not a power of 2</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">cconvolve</span><span class="symbol">(</span><span class="normal">Complex</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// should probably pad x and y with 0s so that they have same length</span>
<span class="normal">        </span><span class="comment">// and are powers of 2</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">!=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Dimensions don't agree"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// compute FFT of each sequence</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// point-wise multiply</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            c</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">times</span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// compute inverse FFT</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">ifft</span><span class="symbol">(</span><span class="normal">c</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the linear convolution of the two specified complex arrays.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x one complex array</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the other complex array</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the linear convolution of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">y</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the length of </span><span class="keyword">&lt;t&gt;</span><span class="comment">x</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> does not equal</span>
<span class="comment">     *         the length of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">y</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or if the length is not a power of 2</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> </span><span class="function">convolve</span><span class="symbol">(</span><span class="normal">Complex</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> Complex</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">*</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">2</span><span class="symbol">*</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            a</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> ZERO</span><span class="symbol">;</span>

<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="number">2</span><span class="symbol">*</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            b</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">2</span><span class="symbol">*</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            b</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> ZERO</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">cconvolve</span><span class="symbol">(</span><span class="normal">a</span><span class="symbol">,</span><span class="normal"> b</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// display an array of Complex numbers to standard output</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">(</span><span class="normal">Complex</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> title</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">title</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"-------------------"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Test client.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">FFT</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Complex</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">// original data</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Complex</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">            x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Complex</span><span class="symbol">(-</span><span class="number">2</span><span class="symbol">*</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">random</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"x"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// FFT of original data</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="function">fft</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"y = fft(x)"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// take inverse FFT</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> z </span><span class="symbol">=</span><span class="normal"> </span><span class="function">ifft</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">(</span><span class="normal">z</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"z = ifft(y)"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// circular convolution of x with itself</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="function">cconvolve</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">(</span><span class="normal">c</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"c = cconvolve(x, x)"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// linear convolution of x with itself</span>
<span class="normal">        Complex</span><span class="symbol">[]</span><span class="normal"> d </span><span class="symbol">=</span><span class="normal"> </span><span class="function">convolve</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">(</span><span class="normal">d</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"d = convolve(x, x)"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
